<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      title="派单"
      width="60%"
      :before-close="handleClose"
    >
      <el-form
        :model="dispatchForm"
        :rules="dispatchFormRules"
        ref="dispatchFormRef"
        label-width="110px"
      >
        <el-form-item label="派单服务" prop="product_id">
          <el-select
            v-model="dispatchForm.product_id"
            placeholder="请选择"
            filterable
            clearable
            @change="changeServiceProduct"
          >
            <el-option
              v-for="item in products"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="派单服务商" prop="provider">
          <el-radio-group v-model="dispatchForm.provider">
            <el-radio
              v-for="(item, index) in providers"
              :key="index"
              :label="item.provider_id"
            >
              {{ item.provider_short_name }}【{{ item.provider_id }}】
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="服务凭证码" prop="evidences">
          <el-input
            v-model="dispatchForm.evidences"
            placeholder="请输入第三方系统下单后获取的凭证码"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button
          class="theme-background-color"
          @click="saveDispatchInfo"
          :disabled="isDisabled"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  props: {
    dispatchDialogVisible: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    orderId: {
      type: Number,
      default: function () {
        return {}
      }
    },
    productId: {
      type: Number,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      products: [],
      providers: [],
      dispatchForm: {
        product_id: '',
        provider: '',
        evidences: ''
      },
      dispatchFormRules: {
        product_id: [{ required: true, message: '请选择', trigger: 'change' }],
        provider: [{ required: true, message: '请选择', trigger: 'change' }],
        evidences: [{ required: true, message: '请输入', trigger: 'blur' }]
      },
      isDisabled: false
    }
  },
  created() {},
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('update:dispatchDialogVisible', false)
          done()
        })
        .catch((_) => {})
    },
    closeDialog() {
      this.$emit('update:dispatchDialogVisible', false)
    },
    async saveDispatchInfo() {
      this.$refs.dispatchFormRef.validate(async (valid) => {
        if (!valid) return
        this.dispatchForm.order_booking_id = this.orderId
        // console.log(this.dispatchForm)

        this.isDisabled = true
        const { data: res } = await this.$http.post(
          '/admin/order_booking/dispatch',
          this.dispatchForm
        )
        this.isDisabled = false
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('派单成功')
        this.visible = false
        this.$emit('update:dispatchDialogVisible', false)
        this.$parent.getDetailData()
      })
    },
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product',
        type: 1
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.products = res.data.product
    },
    async changeServiceProduct(val) {
      const { data: res } = await this.$http.post(
        '/admin/product/getProviderByProductId',
        {
          product_id: val
        }
      )
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.providers = res.data
    }
  },
  watch: {
    dispatchDialogVisible() {
      this.visible = this.dispatchDialogVisible
      if (this.visible) {
        this.getSelectData()

        if (this.productId) {
          this.dispatchForm.product_id = this.productId
          this.changeServiceProduct(this.productId)
        }
      } else {
        this.$refs.dispatchFormRef.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 80%;
  margin-left: 15px;
  .el-select {
    width: 100%;
  }
}
</style>
